import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Pageable, SparrowUrl, SysroleUrlPermissionPK } from '../domain/sparrow';



@Injectable({
  providedIn: 'root'
})
export class UrlService {
  host:string = "http://localhost:8091/api";

  constructor(private httpClient: HttpClient) { }

  public list(pageable: Pageable | any){
    let p:Pageable|any = {};
    p.page=pageable.page -1;
    p.size = pageable.size;
    if(pageable.sort)
      p.sort = pageable.sort;
    return this.httpClient.get<any>(this.host + "/sparrowApis", {params:p});
  }

  public saveBatch(urls: SparrowUrl[]){
    return this.httpClient.post<any>(this.host + "/sparrowApis/batch", urls);
  }

  public save(url: SparrowUrl){
    if(url.id === undefined){
      return this.httpClient.post<SparrowUrl>(this.host + "/sparrowApis", url);
    }else{
      return this.httpClient.patch<SparrowUrl>(this.host + "/sparrowApis/" + url.id, url);
    }
  }

  /**
   * name
   */
  public deleteBatch(ids: string[]|any) {
    return this.httpClient.delete<any>(this.host + "/sparrowApis/batch", {body:ids});
  }

  public delete(id: string) {
    return this.httpClient.delete<any>(this.host + "/sparrowApis/" + id);
  }

  public getPermissionById(ids: string[]|null|any){
    return this.httpClient.post<any>(this.host + "/sparrowApis/getPermissionById", ids);
  }

  public addPermission(ids:SysroleUrlPermissionPK[]){
    return this.httpClient.post<any>(this.host + "/sparrowApis/permissions", ids);
  }

  public delPermission(ids:SysroleUrlPermissionPK[]|any){
    return this.httpClient.delete<any>(this.host + "/sparrowApis/permissions", {body:ids});
  }

}


